#include "wl_utils.h"

#include "wl_malloc.h"
#include "wl_trace.h"
#include <string.h>
#include <math.h>
#include "wl_ext_os.h"
#include "wl_ext_others.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */


static int is_little_endian() {
    wl_uint16 test = 0x1;
    return ((wl_uint8 *)&test)[0] == 1;
}

wl_uint16 wl_ntohs(wl_uint16 netshort) {
    if (is_little_endian()) {
        return ((netshort >> 8) & 0xff) | ((netshort << 8) & 0xff00);
    } else {
        return netshort;
    }
}

void wl_sleep(wl_uint32 seconds) {
    wl_ext_sleep(seconds * 1000);
}

void wl_msleep(wl_uint32 milliseconds) {
    wl_ext_sleep(milliseconds);
}

wl_time wl_utctime(void) {
    return wl_ext_get_timestamp();
}

wl_int32 wl_localtime(wl_tm* tm) {
    return 0;
}

wl_uint32 wl_getElapsedTick(void) {
    return wl_ext_get_elasp_ticks();
}

wl_char* wl_strdup(const wl_char* s) {
    wl_size len = strlen(s) + 1;
    void* new   = wl_malloc(len);
    if (new == wl_null) {
        return wl_null;
    }
    return (wl_char*)memcpy(new, s, len);
}

const wl_double pi = 3.14159265358979324;

//
// Krasovsky 1940
//
// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
const wl_double a  = 6378245.0;
const wl_double ee = 0.00669342162296594323;

static wl_bool outOfChina(wl_double lat, wl_double lon) {
    if (lon < 72.004 || lon > 137.8347)
        return wl_true;
    if (lat < 0.8293 || lat > 55.8271)
        return wl_true;
    return wl_false;
}

static wl_double transformLat(wl_double x, wl_double y) {
    wl_double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
    ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
    return ret;
}

static wl_double transformLon(wl_double x, wl_double y) {
    wl_double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
    ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
    return ret;
}

void wl_wgs84togcj02(wl_double wgLat, wl_double wgLon, wl_double* mgLat, wl_double* mgLon) {
    if (outOfChina(wgLat, wgLon)) {
        *mgLat = wgLat;
        *mgLon = wgLon;
        return;
    }
    {
        wl_double dLat   = transformLat(wgLon - 105.0, wgLat - 35.0);
        wl_double dLon   = transformLon(wgLon - 105.0, wgLat - 35.0);
        wl_double radLat = wgLat / 180.0 * pi;
        wl_double magic  = sin(radLat);
        wl_double sqrtMagic;
        magic     = 1 - ee * magic * magic;
        sqrtMagic = sqrt(magic);
        dLat      = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon      = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
        *mgLat    = wgLat + dLat;
        *mgLon    = wgLon + dLon;
    }
};

#ifndef M_PI
#    define M_PI 3.14159265358979323846
#endif

wl_double deg2rad(wl_double degrees) {
    return degrees * M_PI / 180.0;
}

wl_double wl_getdistance(wl_double lng1, wl_double lat1, wl_double lng2, wl_double lat2) {
    // 将角度转为狐度
    wl_double radLat1 = deg2rad(lat1);  // deg2rad()函数将角度转换为弧度
    wl_double radLat2 = deg2rad(lat2);
    wl_double radLng1 = deg2rad(lng1);
    wl_double radLng2 = deg2rad(lng2);
    wl_double a       = radLat1 - radLat2;
    wl_double b       = radLng1 - radLng2;
    return 2 * asin(sqrt(pow(sin(a / 2), 2) + cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2))) * 6378.137 * 1000;
}

wl_char* wl_strtok_r(wl_char* str, const wl_char* delim, wl_char** saveptr) {
#ifdef WIN32
    return strtok_s(str, delim, saveptr);
#else
    return strtok_r(str, delim, saveptr);
#endif
}

wl_uint32 RSHash(wl_char* str, wl_uint32 len) {
    wl_uint32 b    = 378551;
    wl_uint32 a    = 63689;
    wl_uint32 hash = 0;
    wl_uint32 i    = 0;

    for (i = 0; i < len; str++, i++) {
        hash = hash * a + (*str);
        a    = a * b;
    }

    return hash;
}
/* End Of RS Hash Function */

wl_uint32 JSHash(wl_char* str, wl_uint32 len) {
    wl_uint32 hash = 1315423911;
    wl_uint32 i    = 0;

    for (i = 0; i < len; str++, i++) {
        hash ^= ((hash << 5) + (*str) + (hash >> 2));
    }

    return hash;
}
/* End Of JS Hash Function */

#ifdef __cplusplus
}
#endif /* __cplusplus */
